package com.example.demo1.repository;

import com.example.demo1.pojo.order;
import com.example.demo1.service.orderService;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Repository
public interface orderRepository extends JpaRepository<order,Integer> {
    Optional<order> findByOrderNumber(String orderNumber);

    List<order> findByUserId(Integer userId);

    List<order> findByUserIdAndStatus(Integer userId, Integer status);

    List<order> findByStatus(Integer status);

    List<order> findByProductId(Integer productId);

    @Query("select o from order o where o.createdAt between :startDate and :endDate")
    List<order> findByCreatedAtBetween(@Param("startDate") LocalDateTime startDate,
                                       @Param("endDate") LocalDateTime endDate);

    @Query("select count(o) from order o where o.userId = :userId and o.status = :status")
    long countByUserIdAndStatus(@Param("userId") Integer userId, @Param("status") Integer status);

    @Query("select o from order o join user u on o.userId = u.id join product p on o.productId = p.id where o.id = :id")
    Optional<order> findByIdWithDetails(@Param("id") Integer id);

    @Query("select o from order o join user u on o.userId = u.id join product p on o.productId = p.id where o.userId = :userId")
    List<order> findByUserIdWithDetails(@Param("userId") Integer userId);
}
